﻿Imports LRS.BLL
Imports System.Windows.Forms

Public Class ManageManufacturer
    Private oldRowIndex As Integer = -1
    Private IsAdding As Boolean = False
    Private Sub ManageManufacturer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gridManufacturer.AutoGenerateColumns = False
        gridManufacturer.DataSource = bManufacturer.GetManufacturers()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ucManu.Enabled = True
        ucManu.ClearText()
        IsAdding = True
        btnDelete.Enabled = False
        btnAdd.Enabled = False
        gridManufacturer.Enabled = False
        btnCancel.Enabled = True
        oldRowIndex = -1
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If (gridManufacturer.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn dòng cần xóa")
            Return
        End If
        Dim row As Integer = gridManufacturer.SelectedCells(0).RowIndex
        Dim result As DialogResult
        result = MessageBox.Show("Bạn có chắc chắn muốn xóa nhà sản xuất có Số hiệu: " & gridManufacturer.Rows(row).Cells("ManufacturerNumber").Value, "Xác nhận", MessageBoxButtons.OKCancel)
        If (result = DialogResult.OK) Then
            Dim manID As Integer = gridManufacturer.Rows(row).Cells("ManufacturerID").Value
            bManufacturer.DeleteManufacturerByID(manID)
            gridManufacturer.DataSource = Nothing
            gridManufacturer.DataSource = bManufacturer.GetManufacturers()
            oldRowIndex = -1
        End If
    End Sub

    Private Sub gridManufacturer_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridManufacturer.RowEnter
        If (e.RowIndex = oldRowIndex) Then
            Return
        End If
        oldRowIndex = e.RowIndex
        Dim man As eManufacturer = bManufacturer.GetManufacturerByID(gridManufacturer.Rows(e.RowIndex).Cells("ManufacturerID").Value)
        ucManu.Enabled = True
        ucManu.SetManufacturer(man)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Not ucManu.ucValidateManu()) Then
            Return
        End If
        Dim man As eManufacturer = ucManu.GetManufacturer()
        If (IsAdding) Then
            bManufacturer.Insert(man)
        Else
            bManufacturer.Update(man)
        End If
        gridManufacturer.Enabled = True
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        ucManu.Enabled = False
        btnCancel.Enabled = False
        IsAdding = False
        gridManufacturer.DataSource = Nothing
        gridManufacturer.DataSource = bManufacturer.GetManufacturers()
        Dim i = oldRowIndex
        oldRowIndex = -1
        gridManufacturer.Rows(i).Cells("ManufacturerNumber").Selected = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        gridManufacturer.Enabled = True
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        ucManu.Enabled = False
        btnCancel.Enabled = False
        IsAdding = False

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class